
package system.base;

import com.hitrust.gs.lib.GSBaseConstants;

import framework.core.*;
import system.data.*;
/*
	Author    :chengbiao.he
	CreateTime:2008-02-14 08:38:58
*/
public class ProviderSrv extends IHandler
{

	public ProviderSrv() 
	{
		super();
	}
	private void deleteProvider(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		Provider obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = Provider.fetch(con,ids[i]);
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveProvider(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String id = context.getString("Id",null);
		Provider obj;
		if((id==null)||(id.equals("")))   //add
		{
			obj = Provider.fetch();
    	}
    	else                     //update
    	{
    		obj = Provider.fetch(con,id);
    	}
		ProviderPageData data = (ProviderPageData)PageData.fromForm(context,ProviderPageData.class);
		context.setValue("ProviderPageData",data);
		obj.fill(data);
		obj.save(con);
	}
	private void listProvider(DBConnection con,SrvContext context,String path) throws Exception
	{
		ProviderSearchEvent event = (ProviderSearchEvent)SearchEvent.fromForm(context,ProviderSearchEvent.class);
		event.setStatus(GSBaseConstants.SC_VALID);
		context.setSessionValue("ProviderSearchEvent",event);
		context.setNextUrl(path+"ProviderList.jsp");
	}
	private void backProvider(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"ProviderList.jsp");
	}
	
	private void selectProvider(DBConnection con, SrvContext context, String path) throws Exception
	{
	    boolean multi = context.getBoolean("multi");
	    context.setValue("multi", new Boolean(multi));
	    ProviderSearchEvent event = (ProviderSearchEvent)SearchEvent.fromForm(context, ProviderSearchEvent.class);
	    context.setSessionValue("ProviderSearchEvent", event);
	    context.setNextUrl(path + "ProviderSelect.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listProvider"))
		{
			listProvider(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewProvider"))
		{
			String id = context.getString("Id",null);
			Provider obj = Provider.fetch(con,id);
			context.setValue("Provider",obj);
			context.setNextUrl(path+"ProviderView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteProvider"))
		{
			context.setErrorUrl(path+"ProviderList.jsp");
			deleteProvider(con,context);
			backProvider(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddProvider"))
		{
			Provider obj = Provider.fetch();
			ProviderPageData data = (ProviderPageData)PageData.fromTable(obj,ProviderPageData.class);
			context.setValue("ProviderPageData",data);
			context.setNextUrl(path+"ProviderEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateProvider"))
		{
			String id = context.getString("Id",null);
			Provider obj = Provider.fetch(con,id);
			ProviderPageData data = (ProviderPageData)PageData.fromTable(obj,ProviderPageData.class);
			context.setValue("ProviderPageData",data);
			context.setNextUrl(path+"ProviderEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyProvider"))
		{
			String id = context.getString("Id",null);
			Provider obj = Provider.copy(Provider.fetch(con,id));
			ProviderPageData data = (ProviderPageData)PageData.fromTable(obj,ProviderPageData.class);
			context.setValue("ProviderPageData",data);
			context.setNextUrl(path+"ProviderEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveProvider"))
		{
			context.setErrorUrl(path+"ProviderEdit.jsp");
			saveProvider(con,context,loginContext);
			backProvider(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewProvider"))
		{
			context.setErrorUrl(path+"ProviderEdit.jsp");
			saveProvider(con,context,loginContext);
			Provider obj = Provider.fetch();
			ProviderPageData data = (ProviderPageData)PageData.fromTable(obj,ProviderPageData.class);
			context.setValue("ProviderPageData",data);
			context.setNextUrl(path+"ProviderEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListProvider"))
		{
			backProvider(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("selectProvider")){
		    selectProvider(con, context, path);
		    return;
		}
		
		//add by chengbiao.he start

		//add by chengbiao.he end

		Logger.throwException("ProviderSrv","unknown action_code="+action_code);
	}
}
